//
// Decompiled by Jadx - 507ms
//
package x;

import android.database.CrossProcessCursor;
import android.database.Cursor;
import android.database.CursorWindow;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import b.b;
import cc.c;
import g.d;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

public abstract class a<T, K> {
    public final g.a g;
    public final org.greenrobot.greendao.database.Database h;
    public final boolean i;
    public final b.a<K, T> j;
    public final b<T> k;
    public final c l;
    public final int m;

    public a(g.a aVar) {
        b<T> bVar;
        int i;
        this.g = aVar;
        org.greenrobot.greendao.database.Database aVar2 = aVar.a;
        this.h = aVar2;
        this.i = aVar2.g() instanceof SQLiteDatabase;
        b<T> bVar2 = aVar.i;
        this.j = bVar2;
        if (bVar2 instanceof b) {
            bVar = bVar2;
        } else {
            bVar = null;
        }
        this.k = bVar;
        this.l = aVar.h;
        d dVar = aVar.f;
        if (dVar != null) {
            i = dVar.a;
        } else {
            i = -1;
        }
        this.m = i;
    }

    public static void n(Object obj, org.greenrobot.greendao.database.c cVar) {
        if (obj instanceof Long) {
            cVar.j(1, ((Long) obj).longValue());
        } else {
            if (obj == null) {
                throw new c("Cannot delete entity, key is null");
            }
            cVar.i(1, obj.toString());
        }
        cVar.execute();
    }

    public abstract void a(SQLiteStatement sQLiteStatement, T t);

    public final void aa(Object obj, org.greenrobot.greendao.database.c cVar) {
        b(obj, cVar);
        int length = this.g.d.length + 1;
        Long c = c(obj);
        if (c instanceof Long) {
            cVar.j(length, c.longValue());
        } else {
            if (c == null) {
                throw new c("Cannot update entity without key - was it inserted before?");
            }
            cVar.i(length, c.toString());
        }
        cVar.execute();
        p(c, obj, true);
    }

    public abstract void b(Object obj, org.greenrobot.greendao.database.c cVar);

    public abstract Long c(Object obj);

    public abstract Object d(Cursor cursor);

    public abstract Long e(Cursor cursor);

    public abstract Long f(long j, Object obj);

    public final void o() {
        g.a aVar = this.g;
        if (aVar.e.length == 1) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(this);
        sb.append(" (");
        throw new c(androidx.activity.b.j(sb, aVar.b, ") does not have a single-column primary key"));
    }

    public final void p(K k, T t, boolean z) {
        b.a<K, T> aVar = this.j;
        if (aVar == null || k == null) {
            return;
        }
        if (z) {
            aVar.put(k, t);
        } else {
            aVar.a(k, t);
        }
    }

    public final void q(T t) {
        o();
        Long c = c(t);
        if (c == null) {
            if (t == null) {
                throw new NullPointerException("Entity may not be null");
            }
            throw new c("Entity has no key");
        }
        o();
        org.greenrobot.greendao.database.c j = this.l.j();
        if (this.h.a()) {
            synchronized (j) {
                n(c, j);
            }
        } else {
            this.h.c();
            try {
                synchronized (j) {
                    n(c, j);
                }
                this.h.h();
            } finally {
                this.h.b();
            }
        }
        b.a<K, T> aVar = this.j;
        if (aVar != null) {
            aVar.remove(c);
        }
    }

    public final void r() {
        this.h.d(androidx.activity.b.j(new StringBuilder("DELETE FROM '"), this.g.b, "'"));
        b.a<K, T> aVar = this.j;
        if (aVar != null) {
            aVar.clear();
        }
    }

    public final long s(Object obj, org.greenrobot.greendao.database.c cVar) {
        long j;
        org.greenrobot.greendao.database.Database aVar = this.h;
        if (aVar.a()) {
            j = u(obj, cVar);
        } else {
            aVar.c();
            try {
                long u = u(obj, cVar);
                aVar.h();
                aVar.b();
                j = u;
            } catch (Throwable th) {
                aVar.b();
                throw th;
            }
        }
        if (j != -1) {
            p(f(j, obj), obj, true);
        } else {
            Log.w("greenDAO", "Could not insert row (executeInsert returned -1)");
        }
        return j;
    }

    public final long t(T t) {
        c cVar = this.l;
        if (((org.greenrobot.greendao.database.c) cVar.g) == null) {
            org.greenrobot.greendao.database.c f = ((org.greenrobot.greendao.database.Database) cVar.b).f(d.b("INSERT INTO ", (String) cVar.e, (String[]) cVar.f));
            synchronized (cVar) {
                if (((org.greenrobot.greendao.database.c) cVar.g) == null) {
                    cVar.g = f;
                }
            }
            if (((org.greenrobot.greendao.database.c) cVar.g) != f) {
                f.close();
            }
        }
        return s(t, (org.greenrobot.greendao.database.c) cVar.g);
    }

    public final long u(T t, org.greenrobot.greendao.database.c cVar) {
        synchronized (cVar) {
            if (!this.i) {
                b(t, cVar);
                return cVar.m();
            }
            SQLiteStatement sQLiteStatement = (SQLiteStatement) cVar.l();
            a(sQLiteStatement, t);
            return sQLiteStatement.executeInsert();
        }
    }

    public final void v(fm.c cVar) {
        c cVar2 = this.l;
        if (((org.greenrobot.greendao.database.c) cVar2.h) == null) {
            org.greenrobot.greendao.database.c f = ((org.greenrobot.greendao.database.Database) cVar2.b).f(d.b("INSERT OR REPLACE INTO ", (String) cVar2.e, (String[]) cVar2.f));
            synchronized (cVar2) {
                if (((org.greenrobot.greendao.database.c) cVar2.h) == null) {
                    cVar2.h = f;
                }
            }
            if (((org.greenrobot.greendao.database.c) cVar2.h) != f) {
                f.close();
            }
        }
        s(cVar, (org.greenrobot.greendao.database.c) cVar2.h);
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0054  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final ArrayList w(Cursor cursor) {
        CursorWindow cursorWindow;
        boolean z;
        int count = cursor.getCount();
        if (count == 0) {
            return new ArrayList();
        }
        ArrayList arrayList = new ArrayList(count);
        if (cursor instanceof CrossProcessCursor) {
            cursorWindow = ((CrossProcessCursor) cursor).getWindow();
            if (cursorWindow != null) {
                if (cursorWindow.getNumRows() == count) {
                    cursor = new g.b(cursorWindow);
                    z = true;
                    if (cursor.moveToFirst()) {
                        b.a<K, T> aVar = this.j;
                        if (aVar != null) {
                            aVar.lock();
                            aVar.d(count);
                        }
                        try {
                            if (!z && cursorWindow != null && aVar != null) {
                                x(cursor, cursorWindow, arrayList);
                            } else {
                                do {
                                    arrayList.add(y(cursor, false));
                                } while (cursor.moveToNext());
                            }
                            if (aVar != null) {
                                aVar.unlock();
                            }
                        } catch (Throwable th) {
                            if (aVar != null) {
                                aVar.unlock();
                            }
                            throw th;
                        }
                    }
                    return arrayList;
                }
                Log.d("greenDAO", "Window vs. result size: " + cursorWindow.getNumRows() + "/" + count);
            }
        } else {
            cursorWindow = null;
        }
        z = false;
        if (cursor.moveToFirst()) {
        }
        return arrayList;
    }

    public final void x(Cursor cursor, CursorWindow cursorWindow, ArrayList arrayList) {
        CursorWindow cursorWindow2;
        int numRows = cursorWindow.getNumRows() + cursorWindow.getStartPosition();
        int i = 0;
        while (true) {
            arrayList.add(y(cursor, false));
            int i2 = i + 1;
            if (i2 >= numRows) {
                b.a<K, T> aVar = this.j;
                aVar.unlock();
                try {
                    if (cursor.moveToNext()) {
                        cursorWindow2 = ((CrossProcessCursor) cursor).getWindow();
                    } else {
                        aVar.lock();
                        cursorWindow2 = null;
                    }
                    if (cursorWindow2 != null) {
                        numRows = cursorWindow2.getNumRows() + cursorWindow2.getStartPosition();
                    } else {
                        return;
                    }
                } finally {
                    aVar.lock();
                }
            } else if (!cursor.moveToNext()) {
                return;
            }
            i = i2 + 1;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final Object y(Cursor cursor, boolean z) {
        Object b;
        Object obj;
        b<T> bVar = this.k;
        if (bVar != null) {
            long j = cursor.getLong(this.m + 0);
            g.c cVar = bVar.e;
            if (z) {
                obj = bVar.g(j);
            } else {
                Reference reference = (Reference) cVar.e(j);
                if (reference != null) {
                    obj = reference.get();
                } else {
                    obj = null;
                }
            }
            if (obj != null) {
                return obj;
            }
            Object d = d(cursor);
            if (z) {
                ReentrantLock reentrantLock = bVar.f;
                reentrantLock.lock();
                try {
                    cVar.f(j, new WeakReference(d));
                } finally {
                    reentrantLock.unlock();
                }
            } else {
                cVar.f(j, new WeakReference(d));
            }
            return d;
        }
        b.a<K, T> aVar = this.j;
        if (aVar != null) {
            Long e = e(cursor);
            if (z) {
                b = aVar.get(e);
            } else {
                b = aVar.b(e);
            }
            if (b != null) {
                return b;
            }
            Object d2 = d(cursor);
            p(e, d2, z);
            return d2;
        }
        return d(cursor);
    }

    public final void z(Object obj, SQLiteStatement sQLiteStatement) {
        a(sQLiteStatement, obj);
        int length = this.g.d.length + 1;
        Long c = c(obj);
        if (c instanceof Long) {
            sQLiteStatement.bindLong(length, c.longValue());
        } else {
            if (c == null) {
                throw new c("Cannot update entity without key - was it inserted before?");
            }
            sQLiteStatement.bindString(length, c.toString());
        }
        sQLiteStatement.execute();
        p(c, obj, true);
    }
}
